var express = require("express");
var router = express.Router();

var data = require("./data");

var moviesList = data.slice(0, 10);
var ids = data.map((item) => item.id);
var total = data.length;

router.get("/list", function (req, res, next) {
  res.json({
    success: true,
    data: moviesList,
    ids,
    total,
  });
});

router.post("/more", (req, res, next) => {
  const { ids } = req.body;

  if (ids) {
    if (typeof ids === "string" && ids[0] !== "[") {
      const arr = ids.split(",");
      console.log(arr);

      const result = data.filter((item) => {
        return arr.includes(item.id + "");
      });

      res.json({
        success: true,
        data: result,
      });
    } else {
      res.json({
        success: false,
        msg: "参数错误",
      });
    }
  } else {
    res.json({
      success: false,
      msg: "缺少参数",
    });
  }
});

module.exports = router;
